﻿using MySql.Data.MySqlClient;
using NB.MGJ.FrameWork;
using NB.MGJ.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace NB.MGJ.Service.Origins
{
    public partial  class OriginsMaterialService
    {
        private BaseRepository _baseRepository = new BaseRepository();

        #region 增

        public int Add(Origins_Material model)
        {
            int result = _baseRepository.InsertReturnID<Origins_Material>(model);
            return result;
        }

        #endregion

        #region 删

        public int DelMaterialService(int id)
        {
            var sql = string.Format(" UPDATE Origins_Material  SET ViewDel=1 WHERE MaterialId = {0}", id);
            int result = _baseRepository.ExecuteSql<int>(sql);
            return result;
        }
        #endregion

        #region 改
        public int Update(Origins_Material model)
        {
            int result = _baseRepository.Update<Origins_Material>(model);
            return result;
        }

        #endregion

        #region 查

        public PagedList<Origins_Material> GetPageList(ref PagerModel pager, string where, int eid)
        {

            try
            {
                StringBuilder strSql1 = new StringBuilder();
                strSql1.Append(@" Origins_Material ");
                var strSql = new StringBuilder(strSql1.ToString().Trim(','));
                pager.FldName = " * ";
                pager.TblName = strSql.ToString();


                if (string.IsNullOrEmpty(pager.StrCondition) || pager.StrCondition == " ")
                {
                    pager.StrCondition = " 1=1 ";
                }
                if (string.IsNullOrEmpty(pager.FldSort) || pager.FldSort == "")
                {
                    pager.FldSort = "  ChangeDate desc ";
                    pager.StrCondition += " and ViewDel=0 and EnterpriseId=" + eid;
                }
                MySqlParameter[] parameters = _baseRepository.SqParameterRefactor(ref pager);


                IEnumerable<Origins_Material> resultList = _baseRepository.ExecuteStoredProcedureCollection<Origins_Material>("Common_PageList", parameters);
                return new PagedList<Origins_Material>(resultList.ToList(), pager.Page, pager.PageSize, 1, Convert.ToInt32(parameters[6].Value));
            }
            catch (Exception ex)
            {
                throw ex;
            }




        }



        public List<Origins_Material> GetOriginsTempletList ()
        {
            string sql = @"select * from Origins_Material where MaterialId in (1,2,3,4,5)";
            var model = _baseRepository.ExecuteSqlCollection<Origins_Material>(sql).ToList();
            return model;
        }


       



        public Origins_Material GetModel(int? id)
        {
            string sql = @"select * from Origins_Material where MaterialId=" + id;
            var model = _baseRepository.ExecuteSqlCollection<Origins_Material>(sql).ToList().FirstOrDefault();
            return model;
        }


        #endregion

        #region 其他
        public List<Origins_Material> GetMatetialListByEID(int EID)
        {
            var sql = @"select * from Origins_Material where EnterpriseId=" + EID;
            var list = _baseRepository.ExecuteSqlCollection<Origins_Material>(sql).ToList();
            return list;
        }

        public List<SelectListItem> GetMaterialSelectList(int Materialid, int eid)
        {
            List<Origins_Material> result = GetMatetialListByEID(eid);
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem()
            {
                Text = "",
                Value = ""
            });
            for (int i = 0; i < result.Count; i++)
            {
                SelectListItem item = new SelectListItem()
                {
                    Text = result[i].MaterialName,
                    Value = result[i].MaterialId.ToString()
                };
                if (Materialid == result[i].MaterialId)
                {
                    item.Selected = true;
                }
                items.Add(item);
            }
            return items;
        }




        #endregion


    }
}
